/**
 *
 */
package com.evil.DB_Utils;

import com.evil.DB_Utils.InsertCallBack.Add;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/******************************************
 * 工程名：Download<br>
 * 包名：com.evil.DB_Utils<br>
 * 时间：2016年6月10日 下午12:07:47<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：添加<br>
 ******************************************/
public class Insert extends DB_Utils
{
    private PreparedStatement preparedStatement;
    private String url;

    private int index;
    private String returnError;

    public Insert(String url)
    {
        this.url = url;
        if (url.equals(""))
            System.err.println("url地址不能为空");
        else
        {
            index = 1;
            try
            {
                preparedStatement = (PreparedStatement) connection
                        .prepareStatement(url);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加
     *
     * @param s
     * @return
     */
    public Insert add(String s)
    {
        try
        {
            preparedStatement.setString(index, s);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param i
     * @return
     */
    public Insert add(int i)
    {
        try
        {
            preparedStatement.setInt(index, i);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param date
     * @return
     */
    public Insert add(Date date)
    {
        try
        {
            preparedStatement.setDate(index, date);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param f
     * @return
     */
    public Insert add(float f)
    {
        try
        {
            preparedStatement.setFloat(index, f);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param object
     * @return
     */
    public Insert add(Object object)
    {
        try
        {
            preparedStatement.setObject(index, object);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 需要使用while
     *
     * @param callBack
     * @return
     */
    public Insert addAll(InsertCallBack callBack)
    {
        Add add = new Add();
        callBack.onCallBack(add.set(preparedStatement));
        add.close();
        return this;
    }

    /**
     * 建立链接，必须调用
     *
     * @return
     */
    public Insert build()
    {
        try
        {
            returnError = preparedStatement.executeUpdate() + "";
        }
        catch (SQLException e)
        {
            if (e.toString().indexOf("PRIMARY") > 0)
                returnError = ErrorInfo.ERROR_EXIST_PRIMARY.toString();
            else if (e.toString().endsWith("ON DELETE NO ACTION ON UPDATE NO ACTION)"))
                returnError = ErrorInfo.ERROR_NO_FOREIGN_KEY.toString();
            else if (e.toString().indexOf("No value specified for parameter") > 0)
                returnError = ErrorInfo.ERROR_NOVALUE_FORPARAMETER.toString();
            else if (e.toString().indexOf("Parameter index out of range") > 0)
                returnError = ErrorInfo.ERROR_MOREVALUE_FORPARAMETER.toString();
            else if (e.toString().indexOf("Cannot add or update a child row: a foreign key constraint fails") > 0)
                returnError = ErrorInfo.ERROR_NO_FOREIGN_VALUE.toString();
            else
                e.printStackTrace();
        }

        return this;
    }

    /**
     * 关闭流
     */
    public String close()
    {
        try
        {
            preparedStatement.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return returnError;
    }
}
